package cms.com.yh.servlet;

import cms.common.PageData;
import cms.common.Result;
import cms.mager.service.CustomerService;
import cms.mager.service.impl.CustomerServiceImpl;
import cms.util.HttpCharSet;
import cms.util.InvokeHttp;
import cms.util.respUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能:
 * Author: yuhui
 * Date: 2021/4/9 20:20
 */
public class CustomerServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpCharSet.charSet(req,resp);
        String service = req.getParameter("service");
        InvokeHttp.invokeReq(this,service,req,resp);
    }

    /**
    * 功能描述: 模糊分页查询所有客户信息
    * @Author: yuhui  2021/4/9 20:25
    */
    public void PageInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String company = req.getParameter("company");
        String idu = req.getParameter("user_id");
        Integer user_id = (idu == null || "".equals(idu)) ? null:Integer.valueOf(idu);
        String de = req.getParameter("deleted");
        Integer deleted = (de == null || "".equals(de)) ? null:Integer.valueOf(de);
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer pageSize = Integer.valueOf(req.getParameter("limit"));

        CustomerService sc = new CustomerServiceImpl();
        PageData pd = new PageData();
        pd.setPage(page);
        pd.setPageSize(pageSize);
        Map param = new HashMap();
        param.put("name",name);
        param.put("phone",phone);
        param.put("company",company);
        param.put("user_id",user_id);
        param.put("deleted",deleted);
        Result getpage = sc.getpage(pd, param);
        respUtil.respOutPrint(getpage,resp);
    }

    /**
    * 功能描述: 添加客户
    * @Author: yuhui  2021/4/9 20:37
    */
    public void addInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object[] arr = paresParamForAdd(req);
        CustomerService sc = new CustomerServiceImpl();
        Result res = sc.addData(arr);
        respUtil.respOutPrint(res,resp);
    }

    /**
    * 功能描述: 删除客户
    * @Author: yuhui  2021/4/9 20:38
    */
    public void delInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        CustomerService sc = new CustomerServiceImpl();
        Result res = sc.delData(Integer.valueOf(id));
        respUtil.respOutPrint(res,resp);
    }

    /**
    * 功能描述: 更新客户信息
    * @Author: yuhui  2021/4/9 20:40
    */
    public void updInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String[] upcol = req.getParameter("updCol").split(",");
        Map<String,String> params = new HashMap<>();
        for(String str : upcol){
            params.put(str,req.getParameter(str));
        }
        params.put("id",req.getParameter("id"));

        CustomerService sc = new CustomerServiceImpl();
        Result res = sc.updateData(params,upcol);
        respUtil.respOutPrint(res,resp);
    }

    /**
    * 功能描述: 更新客户的业务员
    * @Author: yuhui  2021/4/10 19:34
    */
    public void setUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CustomerService cs = new CustomerServiceImpl();
        String[] cids = req.getParameterValues("cid");
        String uId = req.getParameter("user_c");
        Result result = cs.setUserToCu(cids, uId);
        respUtil.respOutPrint(result,resp);
    }









    private Object[] paresParamForAdd(HttpServletRequest req){
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        Integer sex = Integer.valueOf(req.getParameter("sex"));
        String company = req.getParameter("company");
        String user_id = req.getParameter("user_id");

        Object[] obj = {name,phone,sex,company,user_id};
        return obj;
    }
}
